package org.example.hospital_registration_system.service;


import org.example.hospital_registration_system.enums.AppointmentStatus;
import org.example.hospital_registration_system.mapper.IAppointmentRelationMapper;
import org.example.hospital_registration_system.mapper.IDepartmentMapper;
import org.example.hospital_registration_system.mapper.IDoctorInfoMapper;
import org.example.hospital_registration_system.mapper.IUserInfoMapper;
import org.example.hospital_registration_system.model.AppointmentRelation;
import org.example.hospital_registration_system.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Random;

@Service
public class AppointmentRelationService {

    @Autowired
    private IAppointmentRelationMapper iAppointmentRelationMapper;

    @Autowired
    private IDepartmentMapper iDepartmentMapper;

    @Autowired
    private IDoctorInfoMapper iDoctorInfoMapper;

    @Autowired
    private IUserInfoMapper iUserInfoMapper;


    public  List<AppointmentRelation> selectByUserId(Integer userId){
        //查询出的列表
        List<AppointmentRelation> appointmentRelations = iAppointmentRelationMapper.selectByUserId(userId);
        if(appointmentRelations==null){
            return null;
        }
        for(AppointmentRelation appointmentRelation:appointmentRelations){
            appointmentRelation.setStatusZT(AppointmentStatus.setStatusByCode(appointmentRelation.getStatus()).getStatus());
        }
        return appointmentRelations;
    }

    public  List<AppointmentRelation> selectByStatus(Integer userId,Integer status){

        List<AppointmentRelation> appointmentRelations = iAppointmentRelationMapper.selectByStatus(userId,status);
        if(appointmentRelations==null){
            return null;
        }

        for(AppointmentRelation appointmentRelation:appointmentRelations){
            appointmentRelation.setStatusZT(AppointmentStatus.setStatusByCode(appointmentRelation.getStatus()).getStatus());
        }
        return appointmentRelations;
    }


    public List<AppointmentRelation> selectByData(Integer userId, String appointmentDate){
        List<AppointmentRelation> appointmentRelations = iAppointmentRelationMapper.selectByData(userId,appointmentDate);
        if(appointmentRelations==null){
            return null;
        }

        for(AppointmentRelation appointmentRelation:appointmentRelations){
            appointmentRelation.setStatusZT(AppointmentStatus.setStatusByCode(appointmentRelation.getStatus()).getStatus());
        }
        return appointmentRelations;
    }

    public String insertData(AppointmentRelation appointmentRelation){
        Integer randomDoctorId = 0;
        Integer departmentId = appointmentRelation.getDepartmentId();
        String departmentName = iDepartmentMapper.getDepartmentNameById(departmentId);
        appointmentRelation.setDepartmentName(departmentName);


        Integer level = appointmentRelation.getLevel();
        List<Integer> doctorIds = iDoctorInfoMapper.getDoctorIdByLevel(level);
        //获取其中一个
        if (!doctorIds.isEmpty()) {
            Random random = new Random();
            randomDoctorId = doctorIds.get(random.nextInt(doctorIds.size()));
        }


        String doctorName = iDoctorInfoMapper.getDoctorNameById(randomDoctorId);
        appointmentRelation.setDoctorId(randomDoctorId);
        appointmentRelation.setDoctorName(doctorName);

        Integer userId = appointmentRelation.getUserId();
        String  userName = iUserInfoMapper.selectUserById(userId);
        appointmentRelation.setUserName(userName);



        Integer n = iAppointmentRelationMapper.insertRelation(appointmentRelation);
        if(n==1){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }

    public  String deleteRelation(Integer relationId){
        Integer n = iAppointmentRelationMapper.deleteRelation(relationId);
        if(n==1){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
}
